package com.srs.dao.info;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.info.Rx;
import com.srs.util.HibernateUtil;

public class RxDao {

	final static Logger logger = LoggerFactory.getLogger(RxDao.class);

	private List<Rx> rxList;
	private Rx rx;

	public void delete(Integer agentId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			rx = (Rx) session.get(Rx.class, agentId);
			session.delete(rx);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Rx> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			rxList = session.createQuery("from Rx").list();
			return rxList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Rx> list(Integer agentId, String fromDate, String toDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Rx");
//			q.setInteger("agentId", agentId);
//			q.setString("fromDate", fromDate);
//			q.setString("toDate", toDate);
			rxList = q.list();
			if (rxList==null){
				return new ArrayList<Rx>();
			}
			
			int index = rxList.size()-1;
			while (index>=0){
				if (rxList.get(index).getAgentId().intValue()!=agentId.intValue()){
					rxList.remove(index);
				}
				index = index - 1;
			}
			
			for (Rx rx : rxList) {
				System.out.println(rx.getAgentId() + rx.getRxAverage() + rx.getOnDate());
			}				
			return rxList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}		
	
	public Rx get(Integer agentId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Rx r where r.agentId=:agentId");
			q.setInteger("agentId", agentId);
			return (Rx) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public Rx get(Integer agentId, String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Rx r where r.agentId=:agentId and r.onDate=:onDate");
			q.setInteger("agentId", agentId);
			q.setString("onDate", onDate);
			return (Rx) q.uniqueResult();
		} finally {
			session.close();
		}

	}	
	
	public void insert(Rx rx) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(rx);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(Rx rx) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(rx);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		RxDao rxDao=new RxDao();
		List<Rx> list = rxDao.list(43, "2009-10-20", "2009-10-28");
		System.out.println(list);
		
	}	
	
}
